package com.shd.edanpei.ui.activity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.bean.QrcodeBean;
import com.shd.edanpei.bean.UrlModel;
import com.shd.edanpei.bean.UserBean;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.presenter.contract.QrcodeContract;
import com.shd.edanpei.presenter.QrcodePresenter;
import com.shd.edanpei.util.ToastUtil;

import java.io.File;
import java.io.FileOutputStream;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的二维码
 */
public class QrcodeActivity extends BaseActivity<QrcodePresenter>
        implements QrcodeContract.QrcodeIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_headline_right)
    ImageView ivHeadlineRight;
    @BindView(R.id.civ_my_qrcode)
    CircleImageView civHead;
    @BindView(R.id.tv_qrcode_name)
    TextView tvQrcodeName;
    @BindView(R.id.iv_myqrcode_pic)
    ImageView ivQrcode;
    @BindView(R.id.rl_qrcode_pic)
    RelativeLayout rlQrcodePic;
    @BindView(R.id.iv_qrcodeBg)
    ImageView ivQrcodeBg;

    private String mQrcodePicUrl;
    private File file;

    @BindView(R.id.scrollView)
    ScrollView scrollView;

    @Override
    protected int setLayout() {
        return R.layout.activity_qrcode;
    }

    @Override
    protected QrcodePresenter bindPresenter() {
        return new QrcodePresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        loadImageCtrl = new LoadImageCtrl(this);
        headline.setText("我的二维码");
        ivHeadlineRight.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_twoword_share));
        ivHeadlineRight.setVisibility(View.VISIBLE);
        UserBean userEntity = DaoManager.getInstance().getUserBean();
        if (TextUtils.isEmpty(userEntity.getAvatarUrl())) {
            civHead.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_default_head));
        } else {
            loadImageCtrl.getGlide().load(userEntity.getAvatarUrl()).into(civHead);
        }
        tvQrcodeName.setText(userEntity.getRealName());
        mPresenter.getQrcode();
    }

    private void saveView() {
        int width = rlQrcodePic.getWidth();
        int height = rlQrcodePic.getHeight();
//        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Bitmap bitmap = null;
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        rlQrcodePic.draw(canvas);


        String path = Environment.getExternalStorageDirectory().getPath() + "/DCIM/";
        file = new File(path, "haha.jpg");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bitmap.recycle();
        }
    }

    @Override
    protected void initListener() {

    }


    private void saveScrollView() {
        int h = 0;
        Bitmap bitmap = null;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            h += scrollView.getChildAt(i).getHeight();
            scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
        }
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);

        String path = Environment.getExternalStorageDirectory().getPath() + "/DCIM/";
        file = new File(path, "haha.jpg");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bitmap.recycle();
        }

    }

    @OnClick({R.id.iv_headline_back, R.id.iv_headline_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_headline_back:
                finish();
                break;
            case R.id.iv_headline_right:
                if (!TextUtils.isEmpty(mQrcodePicUrl)) {
                    saveScrollView();
                    UrlModel urlModel = new UrlModel();
                    urlModel.setPicFile(file.getPath());
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.TYPE, Constant.SHARE_PIC);
                    bundle.putParcelable(Constant.DATA, urlModel);
                    ShareDialogActivity.start(this, bundle);
                } else {
                    ToastUtil.showToast("二维码获取失败");
                }
                break;
        }
    }


    @Override
    public void setQrcode(QrcodeBean qrcodeBean) {
        mQrcodePicUrl = qrcodeBean.getResult();
        loadImageCtrl.getGlide().load(mQrcodePicUrl).into(ivQrcode);

        loadImageCtrl.getGlide().load(qrcodeBean.getBackgroundPic()).into(ivQrcodeBg);
    }
}
